var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();

Page({
  data: {
    util: util,
    orgInfo: '',
    org_name: '',
    bottom_nav_data: [],
    isIphone_x: false,
  
    screen_w: 320,
    screen_h: 480,

    photo_w: 90,
    photo_h: 135,

    video_w: 140,
    video_h: 92,

    msg_img_w: 290,
    msg_img_h: 110,

    userInfo: '',
    photoArr: [],
    videoArr: [],

    zhaValue: 0,
    zanValue: 0,
    zhaProgress_w: 10,
    zanProgress_w: 10,

    showVideo: false,

    hasMsgCount: 0
  },

  cuid: 0,
  token: '',
  inviteuid: '',
  userPhotos: [],

  maxProgress_w: 200,
  min_meili_count: 168,
  max_meili_count: 888,

  onLoad: function (data) {

    this.initIndexView(data);

    var that = this
    app.getWxCheck(function(show) {
      that.setData({
        showVideo: show
      })
    }, true)
  },

  initIndexView: function (data) {
    var that = this;
    //初始化底部导航
    app.getBottomNavData('mine/mine', function (nav_data) {
      that.setData({
        bottom_nav_data: nav_data,
        nav_bgColor: nav_data[0].bgcolor
      });
    });

    //是否是从分享进来的
    this.inviteuid = data.inviteuid ? data.inviteuid : '';
    if (this.inviteuid && this.inviteuid.length > 0) {
      tt.setStorageSync(util.inviteKey, this.inviteuid);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  onShow: function () {
    var that = this
    app.initScreenInfo(function (res) {
      if (res) {
        that.maxProgress_w = app.screen_w - 163;
        var p_w = (app.screen_w - 50) / 3;
        that.setData({
          isIphone_x: app.isIphone_x,
          screen_w: app.screen_w,
          screen_h: app.screen_h,
          photo_w: p_w,
          photo_h: p_w * 1.5,
          video_w: (app.screen_w - 40) / 2,
          video_h: (app.screen_w - 40) / 2 * 0.5625,
          msg_img_w: app.screen_w,
          msg_img_h: (app.screen_w - 30) * 0.378,
          zhaProgress_w: that.maxProgress_w
        });
      }
    })
    this.getLoginUserInfo(true);
  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    tt.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

  goAtherPage: function (e) {
    console.log("goAtherPage!!!!!!!!");
    var url = e.currentTarget.dataset.link;
    // if (url.indexOf("chongzhi/chongzhi") > 0) {

    // } else {
      tt.navigateTo({
        url: url
      })
    // }
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    tt.redirectTo({
      url: url
    })
  },

  //页面分享
  onShareAppMessage: function () {
    var that = this;
    var shareTitle = "我在这里认识很多单身朋友，总有你中意的！"
    var shareImgUrl = that.getShareImgUrl()
    var sharePath = '/pages/launch/launch?share=true&uid=' + that.cuid
    if (!shareImgUrl || shareImgUrl.length <= 0) {
      if(that.data.showVideo) {
        shareImgUrl = util.defaultShareImgUrl
      }
      sharePath = '/pages/launch/launch?goliaota=true'
    }
    if (!that.data.showVideo) {
      shareTitle = "朋友，我发现一些美食和好住处"
      sharePath = '/pages/index/index'
    }
    return {
      title: shareTitle,
      imageUrl: shareImgUrl,
      path: sharePath
    };
  },


  //底部导航跳转
  navigtionClick: function (e) {
    console.log("点击参数：", e);
    var path = e.currentTarget.dataset.path;
    console.log("跳转连接信息：", path);
    if (path) {
      if (path.indexOf('navto:') == 0) {
        var navPath = path.slice(6);
        tt.navigateTo({
          url: navPath,
        })
      } else {
        tt.redirectTo({
          url: path
        });
      }
    }
  },

  allYuyueKcIds: '',
  getLoginUserInfo: function (reAuth) {
    var that = this;
    //读取登录用户信息
    console.log("获取登录用户信息：");
    app.LoginUserInfo(function (res) {
      console.log("登录用户信息：", res);
      if (res && res != -1) {
        that.cuid = res.uid
        that.token = res.token
        that.setData({
          userInfo: res,
          hasMsgCount: parseInt(res.scorecoin) - parseInt(res.scorenum)
        });
        that.getAllUpladedImages()
        that.getAllUpladedVideos()
        
        let userBad = (res.userbad && res.userbad.length > 0) ? parseInt(res.userbad) : 0
        let userRec = (res.userrec && res.userrec.length > 0) ? parseInt(res.userrec) : 0
        that.calculateMeili(userBad, userRec);
      }
    }, reAuth);
  },

  //获取该用户所有已上传的图片信息
  getAllUpladedImages: function(reget = false) {
    var that = this;
    app.getAllUpladedImages(that.cuid, reget, function(res){
      if(res != 0 && res != -1) {
        that.userPhotos = []
        res.forEach(function(m){
          that.userPhotos = that.userPhotos.concat(m.url)
        })
        that.setData({
          photoArr: res,
        })
      }
    })
  },

  //获取该用户所有已上传的视频信息
  getAllUpladedVideos: function(reget = false) {
    var that = this;
    app.getAllUpladedvideos(that.cuid, reget, function(res){
      if(res != 0 && res != -1) {
        that.setData({
          videoArr: res,
        })
      }
      tt.hideLoading();
    })
  },

  showBigPhoto: function(e) {
    let photoIndex = e.currentTarget.dataset.index
    console.log("显示大图：" + photoIndex, e)
    var that = this
    tt.previewImage({
      current: that.userPhotos[photoIndex],
      urls: that.userPhotos
    })
  },

  calculateMeili: function(zhaVal, zanVal){
    let maxValue = zhaVal > zanVal ? zhaVal : zanVal
    maxValue = maxValue < 100 ? 100 : maxValue
    let zha_w = this.maxProgress_w * zhaVal / maxValue
    zha_w = zha_w <= 0 ? 1 : zha_w
    let zan_w = this.maxProgress_w * zanVal / maxValue
    zan_w = zan_w <= 0 ? 1 : zan_w
    this.setData({
      zhaProgress_w: zha_w,
      zanProgress_w: zan_w,
      zhaValue: zhaVal > 9999 ? '9999+' : zhaVal,
      zanValue: zanVal > 9999 ? '9999+' : zanVal,
    })
  },

  showUserQRCode() {
    return
    
    var params = "/pages/launch/launch?share=true&uid=" + this.cuid
    var shareImgUrl = this.getShareImgUrl()
    if (!shareImgUrl || shareImgUrl.length <= 0) {
      params = '/pages/launch/launch?goliaota=true'
    }
    params = encodeURIComponent(params)
    var qrCodeUrl = "http://admin.jawanology.com/wxxcxcode.php?wxacode=1&binduid=219268&path=" + params
    console.log("个人二维码链接：" + qrCodeUrl)
    tt.previewImage({
      current: qrCodeUrl,
      urls: [qrCodeUrl]
    })
    tt.showToast({
      title: '长按可保存',
    })
  },

  getShareImgUrl() {
    for (var i = 0; i < this.data.photoArr.length; i++) {
      var item = this.data.photoArr[i]
      console.log("分享图片链接：", item)
      if(item.pass == '1') {
        return item.url
      }
    }
    return ''
  },

  signIn() {
    tt.showLoading({
      mask: true
    })
    // 签到(增加积分)等接口、 post请求 go.php ac=douyinQiandao 传入 uid-用户id，增加积分，
    var privatekey = utilMd5.hexMD5(this.cuid + "douyinQiandao" + this.token);
    var data = {
      "ac": "douyinQiandao",
      "uid": this.cuid,
      "privatekey": privatekey,
    };
    console.log("签到参数：", data);
    util.api_go({
      data: data,
      method: 'POST',
      success: function (responseData) {
        console.log("签到返回：", responseData);
        tt.hideLoading()
        var data = responseData.data;
        if (data.res) {
          tt.showToast({
            title: "签到成功"
          });
          that.getLoginUserInfo(true)
        } else {
          app.dialog(data.msg, 'loading');
        }
      }
    });
  }

})